var MsgUser = sessionStorage.getItem("user");
MsgUer = JSON.parse(MsgUser);
$(function () {
    initData(MsgUer);
    //删除
    deleteById();
});

//初始化页面
function initData(obj) {
    $.get(
        "../HtmlOrderAllByUserId",
        {userid:obj.id},
        function (msg) {
            let orders = msg.data;
            console.log(orders);
            for (let i = 0; i < orders.length; i++) {
                let tr = $(`
        <div class="row" >
                    <div class="col-6">
						<div class="form-group form-row my-0">
							<label class="col-form-label">订单号</label>
							<div class="col form-control-plaintext">
								<span class="text-primary">${orders[i].id}</span>
							</div>
						</div>
					</div>

					<div class="col-6">
						<div class="form-group form-row my-0">
							<label class="col-form-label">收货人</label>
							<div class="col form-control-plaintext">
								${orders[i].username} [<span class="text-primary">${orders[i].tel}</span>]
							</div>
						</div>
					</div>
				</div>

				<div class="row">
					<div class="col-6">
						<div class="form-group form-row my-0">
							<label class="col-form-label">订单状态</label>
							<div class="col form-control-plaintext">
								<span class="text-success">
								        ${orders[i].status == 1 ? "发货中" : ""}
                                        ${orders[i].status == 2 ? "送货中" : ""}
                                        ${orders[i].status == 3 ? "已收货" : ""}
                                        ${orders[i].status == 4 ? "您的快递真正被攻击" : ""}
                                        ${orders[i].status == 5 ? "商家跑路了" : ""}
                                </span>
							</div>
						</div>
					</div>

					<div class="col-6">
						<div class="form-group form-row my-0">
							<label class="col-form-label">订单金额</label>
							<div class="col form-control-plaintext">￥${orders[i].totalamount}</div>
						</div>
					</div>
				</div>

				<div class="row">
					<div class="col-6">
						<div class="form-group form-row my-0">
							<label class="col-form-label">订单时间</label>
							<div class="col form-control-plaintext">${orders[i].creationtime}</div>
						</div>
					</div>

					<div class="col-6">
						<div class="form-group form-row my-0">
							<label class="col-form-label">收货地址</label>
							<div class="col form-control-plaintext">${orders[i].address}</div>
						</div>
					</div>
				</div>

				<div class="d-flex justify-content-end my-2">
					<div class="btn-group btn-group-sm">
						<a href="#order-detail-${orders[i].id}" data-toggle="collapse" class="btn btn-outline-secondary">查看订单明细</a>
						<div class="btn-group btn-group-sm dropdown">
							<a href="" data-toggle="dropdown" class="btn btn-outline-secondary dropdown-toggle">订单状态</a>
							<div class="dropdown-menu dropdown-menu-right">
								<a href="#" class="dropdown-item">已下单</a>
								<a href="#" class="dropdown-item">配货中</a>
								<a href="#" class="dropdown-item active">运送中</a>
								<a href="#" class="dropdown-item">等等确认收货</a>
								<a href="#" class="dropdown-item">已确认收货</a>						
						</div>
						<a href="#" class="btn btn-outline-danger deleteById" data-id="${orders[i].id}">删除订单</a>
					</div>
				</div>
            </div>
            
             <div id="order-detail-${orders[i].id}" class="collapse collapsed">
					<div class="order-detail">
						
					</div>
				</div>
               `);
                //详情
                let sons=orders[i].sons;
                for (let j = 0; j < sons.length; j++) {
                    var son=sons[j];
                    let trs=$(`
                        <div>
                            <span class="text-muted">${son.productname}</span>
                            <span class="text-primary">&nbsp;x${son.productcount}</span>
                            <span class="text-danger">&nbsp;￥${son.productcount*son.productcrice}</span>
                        </div>
                       、
                   `);
                    tr.find(".order-detail").append(trs);
                }
                //追加
                $("#order-div").append(tr);
            }
        },
        "json"
    )
}

//删除订单
function deleteById() {
   $("#order-div").on("click","div > div > div > .deleteById",function (){
       var id = $(this).data("id");
       $.get(
           "../HtmlOrderByIdDelete",
           {id:id},
           function (msg){
               if (msg.data==1){
                   location.reload();
               }else {
                   alert("删除失败");
               }
           },
           "json"
       )
   });
}
